﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using Microsoft.Cmp.MailService;
using Microsoft.SisparkSaaS.DAL.Common;
using Microsoft.Cmp.Framework;

namespace Microsoft.Cmp.SendMailService
{
    public partial class SendMailService : ServiceBase
    {
        Thread _thread = null;
        public SendMailService()
        {
            InitializeComponent();
        }
        public void Start(string[] args)
        {
            OnStart(args);
        }

        protected override void OnStart(string[] args)
        {
            _thread = new Thread(SendMail);
            _thread.Start();
        }

        protected override void OnStop()
        {
            Log.Info(GetType().FullName, "发送邮件服务运行结束。");
            _thread.Abort();
        }
        private static void SendMail()
        {
            string ThreadSleep = ConfigurationManager.AppSettings["ThreadSleepTime"].ToString();
            while (true)
            {
                try
                {
                    var sendEMail = new SendEMail(new WSBO_SendNoticeEmailDAL());
                    sendEMail.Execute();
                }
                catch (Exception ex)
                {
                    //记录Windows异常
                    Log.Error("发送邮件服务异常",
                              string.Format("错误描述：{0}，堆栈信息：{1}", ex.Message, ex.StackTrace),
                              string.Empty,
                              System.Diagnostics.TraceEventType.Error);
                }
                Thread.Sleep(int.Parse(ThreadSleep));
            }
        }
    }
}
